package com.yl.chainStore.entity.pay

import com.alibaba.druid.support.json.JSONUtils
import com.alibaba.fastjson.JSONObject
import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.entity.OtherShow
import com.yl.chainStore.payrule.order.OrderRule
import com.yl.chainStore.payrule.order.OrderRuleTypeEnum
import com.yl.common.annotation.NoArg
import com.yl.common.getContext
import java.util.*

/**
 * 付款规则数据
 */
@NoArg
@TableName(value = "pay_order_discount_rule",autoResultMap = true)
data class PayOrderDiscountRule(

	/**
	 * 付款规则
	 */
	@TableId(value = "id", type = IdType.AUTO)
	var id: Long,

	/**
	 * 类型
	 */
	@TableField("type")
	var type: OrderRuleTypeEnum = OrderRuleTypeEnum.DEFAULT,


	/**
	 * 状态
	 */
	@TableField("status")
	var status: Status = Status.CLOSE,

	/**
	 * 开始时间
	 */
	@TableField("start_time")
	var startTime: Date,

	/**
	 * 结束时间
	 */
	@TableField("end_time")
	var endTime: Date,

	/**
	 * 排序（优先级）
	 */
	@TableField("order_num")
	var orderNum: Int,

	/**
	 * 公司id
	 */
	@TableField("company_id")
	var companyId: Long = getContext().companyId,

	/**
	 * 分部id
	 */
	@TableField("sub_company_id")
	var subCompanyId: Long? = null,

	@TableField("title")
	var title: String,

	@TableField("enabled")
	var enabled: Int = 1,
):OtherShow(){
	fun getOtherContentObj(): OrderRule {
		return if(other is Map<*, *>){
			JSONObject.parseObject(JSONUtils.toJSONString(other), type.clazz)
		}else{
			JSONObject.parseObject(other.toString(), type.clazz)
		}
	}


	val statusShow:Status
		get() {
			return if(status != Status.OPEN){
				status
			}else{

				when {
					startTime.after(Date()) -> {
						Status.WAITING
					}
					endTime.before(Date()) -> {
						Status.END
					}
					else -> {
						status
					}
				}

			}
		}
}
